package com.netty.demo.server.netty.processor;

import com.netty.demo.server.ApplicationUtil;
import com.netty.demo.server.netty.annotation.ProtocolAnnotation;
import com.netty.demo.server.netty.enums.RequestHeaderProtocol;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略工厂
 *
 * @author Administrator 余秀良
 * @version 1.0
 * <p>date: 2019年09月25日  10:27 </p>
 * <p>address: 四川成都 </p>
 */
@Slf4j
@Component
public class ProtocolProcessorFactory {
    private static Map<RequestHeaderProtocol, ProtocolProcessor> protocolProcessorMap = new ConcurrentHashMap<>();

    public ProtocolProcessorFactory() {
        Map<String, Object> beansWithAnnotation = ApplicationUtil.getApplicationContext().getBeansWithAnnotation(ProtocolAnnotation.class);
        for (Object commandProcessor : beansWithAnnotation.values()) {
            ProtocolAnnotation annotation = commandProcessor.getClass().getAnnotation(ProtocolAnnotation.class);
            protocolProcessorMap.put(annotation.value(), (ProtocolProcessor) commandProcessor);
        }
    }

    public static ProtocolProcessor getProtocolProcessor(RequestHeaderProtocol protocol) {
        return protocolProcessorMap.get(protocol);
    }
}
